import 'package:flutter/material.dart';
import 'package:stage_app/constants/color_constant.dart';

class NumberTextField extends StatefulWidget {
  //高度
  final double height;

  //输入框的宽度 总体宽度为自适应
  final double width;

  //按钮的宽度
  final double iconWidth;
  //按钮的宽度
  final Color iconColor;
  //按钮的宽度
  final Color iconBgColor;

  //默认输入框显示的数量
  final String numText;
  //默认输入框显示的size
  final double textSize;

  //点击加号回调 数量
  final ValueChanged addValueChanged;

  //点击减号回调 数量
  final ValueChanged subValueChanged;

  //点击加减号任意一个回调 数量
  final ValueChanged updateValueChanged;

  NumberTextField({
    Key key,
    this.height = 30,
    this.width = 40,
    this.iconWidth = 40,
    this.iconColor = Colors.white,
    this.iconBgColor = ColorConstant.app_main,
    this.numText = '0',
    this.textSize = 12,
    this.addValueChanged,
    this.subValueChanged,
    this.updateValueChanged,
  }) : super(key: key);

  @override
  createState() => _NumberTextFieldState();
}

class _NumberTextFieldState extends State<NumberTextField> {
  TextEditingController _textEditingController = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this._textEditingController.text = widget.numText;
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        Container(
          height: widget.height,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(4),
              border: Border.all(width: 1, color: Colors.black12)),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              //减号
              CoustomIconButton(icon: Icons.remove, isAdd: false),
              //输入框
              Container(
                width: widget.width,
                decoration: BoxDecoration(
                    border: Border(
                        left: BorderSide(width: 1, color: Colors.black12),
                        right: BorderSide(width: 1, color: Colors.black12))),
                child: TextField(
                  controller: _textEditingController,
                  keyboardType: TextInputType.number,
                  textAlign: TextAlign.center,
                  style: TextStyle(fontSize: widget.textSize),
                  enableInteractiveSelection: false,
                  decoration: InputDecoration(
                    contentPadding:
                        EdgeInsets.only(left: 0, top: 2, bottom: 2, right: 0),
                    border: const OutlineInputBorder(
                      gapPadding: 0,
                      borderSide: BorderSide(
                        width: 0,
                        style: BorderStyle.none,
                      ),
                    ),
                  ),
                ),
              ),
              //加号
              CoustomIconButton(icon: Icons.add, isAdd: true),
            ],
          ),
        )
      ],
    );
  }

  Widget CoustomIconButton({IconData icon, bool isAdd}) {
    return Container(
      width: widget.iconWidth,
      height: widget.iconWidth,
      color: widget.iconBgColor,
      child: IconButton(
        color: widget.iconColor,
        padding: EdgeInsets.all(0),
        iconSize: widget.textSize,
        icon: Icon(icon),
        onPressed: () {
          var num = int.parse(_textEditingController.text);
          if (!isAdd && num == 0) return;
          if (isAdd) {
            num++;
            if (widget.addValueChanged != null) widget.addValueChanged(num);
          } else {
            num--;
            if (widget.subValueChanged != null)
              widget.subValueChanged(num);
          }
          _textEditingController.text = '$num';
          if (widget.updateValueChanged != null) widget.updateValueChanged(num);
        },
      ),
    );
  }
}
